<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>表单</title>
    <link rel="stylesheet" href="__TPL__/frame/layui/css/layui.css">
    <link rel="stylesheet" href="__TPL__/frame/static/css/style.css">
    <link rel="icon" href="__TPL__/frame/static/image/code.png">
</head>
<body class="body">

<form class="layui-form" action="">

    <div class="layui-form-item">
        <label class="layui-form-label">用户组名</label>
        <div class="layui-input-inline">
            <input type="text" name="id" value="{$group.id}" style="display: none" autocomplete="off"
                   class="layui-input">
            <input type="text" name="name" value="{$group.name}" autocomplete="off" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-inline">
            <textarea name="describe" placeholder="请输入内容" class="layui-textarea">{$group.describe}</textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">可视菜单</label>
            <div class="layui-input-block" id="parent">
                {volist name="all_menus" id="vo"}
                {in name="vo.id" value="$menus"}
                <input type="checkbox" name="menu_id" lay-skin="primary" title="{$vo.text}" val="{$vo.id}" checked="">
                {notempty name="vo.subset"}
                {volist name="vo.subset" id="sub"}
                {in name="sub.id" value="$menus"}
                <input type="checkbox" name="menu_id" lay-skin="primary" title="{$sub.text}" val="{$sub.id}" checked="">
                {/in}
                {/volist}
                {/notempty}
                {/in}
                {/volist}
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新增菜单</label>
        <div class="layui-input-inline">
            <select lay-filter="menu_add">
                <option value="0">新增可视菜单</option>
                {volist name="all_menus" id="vo"}
                <option value="{$vo.id}">{$vo.text}</option>
                    {notempty name="vo.subset"}
                        {volist name="vo.subset" id="sub"}
                            <option value="{$sub.id}">--&nbsp;{$sub.text}</option>
                        {/volist}
                    {/notempty}
                {/volist}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">创建时间</label>
            <div class="layui-input-block">
                <input type="text" value="{$group.create_time}" disabled autocomplete="off"
                       class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="edit_submit">立即修改</button>
        </div>
    </div>
</form>

<script src="__TPL__/frame/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'jquery'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.jquery;
        //事件监听
        form.on('select(menu_add)', function (opt) {
            if (opt.value != '0') {
                var id = opt.elem.value;
                var obj_list = $('[name=menu_id]');
                var id_list = [];
                obj_list.each(function () {
                    if ($(this).attr('val'))
                        id_list.push($(this).attr('val'));
                });
                if ($.inArray(id, id_list) === -1) {
                    var text = opt.elem.selectedOptions[0].innerText;
                    var ipt_str = "<input type='checkbox' name='menu_id' lay-skin='primary' title='" + text + "' val='" + id + "' checked>";
                    $('#parent').before(ipt_str);
                    form.render();
                }else{
                    layer.msg('已存在此菜单',{time:1000});
                }
            }
        });
        form.on('submit(edit_submit)',function (data) {
//            console.log(data.field);
            var menu_obj_list = $('[name=menu_id]:checked');
            var temp_menu = [];
            menu_obj_list.each(function () {
                temp_menu.push($(this).attr('val'))
            });
            data.field.menu_ids = temp_menu;
            $.ajax({
                url:'{:url("Auth/editGroupPost")}',
                type:'post',
                data:data.field,
                dataType:'json',
                success:function (data) {
                    if (data.code === 1){
//                        parent.dataTable.reload();
                        var index=parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }else{
                        layer.msg(data.msg, {icon: 1, time: 2000})
                    }
                }
            });
            return false;
        })
    });
</script>
</body>
</html>